package com.lsq.wuyuge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lsq.wuyuge.util.JwtUtil;
import com.lsq.wuyuge.dto.UserInfoDTO;
import com.lsq.wuyuge.dto.UserLoginDTO;
import com.lsq.wuyuge.entity.User;
import com.lsq.wuyuge.mapper.UserMapper;
import com.lsq.wuyuge.propertis.JwtPropertis;
import com.lsq.wuyuge.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 一声闷雷
 * @since 2025-03-15
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper  userMapper;
    @Autowired
    private JwtPropertis jwtPropertis;
    /**
     * 用户登录
     * @param userLoginDTO
     * @return
     */
    @Override
    public UserInfoDTO userLogin(UserLoginDTO userLoginDTO) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        QueryWrapper<User> wrapper = userQueryWrapper
                .eq("username", userLoginDTO.getUsername())
                .eq("password", userLoginDTO.getPassword());
        User user = userMapper.selectOne(wrapper);
        if (user == null) {
            throw new RuntimeException("用户名或密码错误！");
        }
        UserInfoDTO userInfoDTO = new UserInfoDTO();
        BeanUtils.copyProperties(user,userInfoDTO);
        //jwt
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId",user.getId());
        String token= JwtUtil.createToken(
                jwtPropertis.getSecretKey(),
                jwtPropertis.getTtlMillis(),
                claims);
        log.info("token:{}",token);
        userInfoDTO.setToken(token);
        return userInfoDTO;
    }

    /**
     * 更新用户信息

     * @param user
     */
    @Override
    public Boolean updateByUser(User user) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        QueryWrapper<User> wrapper = userQueryWrapper.eq("id", user.getId());
        User user1 = userMapper.selectOne(wrapper);
        if (user1 == null) {
            return false;
        }
        user1.setNickname(user.getNickname());
        user1.setEmail(user.getEmail());
        user1.setBio(user.getBio());
        user1.setUpdateTime(LocalDateTime.now());
        userMapper.updateById(user1);
        return true;
    }

    /**
     * 修改用户密码
     * @param oldPassword
     * @param newPassword
     */
    @Override
    public void updatePassword(String oldPassword, String newPassword, Integer userId) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        QueryWrapper<User> wrapper = userQueryWrapper.eq("id", userId);
        User user = userMapper.selectOne(wrapper);
        if(user.getPassword().equals(oldPassword)){
            user.setPassword(newPassword);
            user.setUpdateTime(LocalDateTime.now());
            userMapper.updateById(user);
        }else{
            throw new RuntimeException("旧密码错误！");
        }
    }

    /**
     * 更新用户头像
     * @param userId
     */
    @Override
    public void updateAvatar(String avatar,Integer userId) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        QueryWrapper<User> wrapper = userQueryWrapper.eq("id", userId);
        User user = userMapper.selectOne(wrapper);
        user.setUpdateTime(LocalDateTime.now());
        user.setAvatar(avatar);
        userMapper.updateById(user);
    }

}
